package com.woniuxy.service.impl;

import cn.hutool.core.bean.BeanUtil;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.mapper.CategoryMapper;
import com.woniuxy.pojo.Category;
import com.woniuxy.pojo.dto.CategoryDTO;
import com.woniuxy.pojo.vo.CategoryVO;
import com.woniuxy.service.CategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author 29331
* @description 针对表【t_category(商品品类管理表)】的数据库操作Service实现
* @createDate 2025-05-29 20:28:41
*/
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category>
    implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;



    @Override
    public Page<Category> getAll(Integer pageSize, Integer pageNum) {
       Page<Category> page = new Page<>(pageNum,pageSize,true);
       LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
       wrapper.orderByAsc(Category::getSortOrder);

        return categoryMapper.selectPage(page,wrapper);
    }

    @Override
    public void addCategory(Category category) {
        category.setDeleted(0);
        categoryMapper.insert(category);
    }

    @Override
    public CategoryVO getCategoryById(Long categoryId) {
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Category::getId,categoryId);
        Category category = categoryMapper.selectOne(wrapper);
        CategoryVO categoryVO = new CategoryVO();
        BeanUtil.copyProperties(category,categoryVO);
        return categoryVO;
    }

    @Override
    public void updateCategory(Category category , Long id) {
      LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
      wrapper.eq(Category::getId,id);


        categoryMapper.update(category,wrapper);
    }

    @Override
    public void deleteCategoryById(Long id) {
        categoryMapper.deleteById(id);

    }

    @Override
    public void removeCategoryByIds(List<Long> ids) {
        LambdaUpdateWrapper<Category> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.in(Category::getId,ids)
                .set(Category::getDeleted,1);
        categoryMapper.update(null,updateWrapper);
    }



    @Override
    public List<String> getCategoryName() {
        List<Category> categories = categoryMapper.selectList(null);
        List<String> names = new ArrayList<>();
        categories.stream().map(category->{
            String categoryName = category.getCategoryName();
            names.add(categoryName);
            return categoryName;
        }).collect(Collectors.toList());
        return names;
    }
    @Override
    public String getSubByCategory(String categoryName) {
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Category::getCategoryName,categoryName);
        Category categories = categoryMapper.selectOne(wrapper);
        String subCategories = categories.getSubCategories();

        return subCategories;
    }

    @Override
    public List<CategoryVO> getByCategoryAndTime(CategoryDTO categoryDTO) {
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();

        // 模糊匹配品类名称
        if (categoryDTO.getCategoryName() != null && !categoryDTO.getCategoryName().isEmpty()) {
            wrapper.eq(Category::getCategoryName, categoryDTO.getCategoryName());
        }

        // 时间范围筛选
        if (categoryDTO.getStartDate() != null && categoryDTO.getEndDate() != null) {
            wrapper.between(Category::getCreateTime, categoryDTO.getStartDate(), categoryDTO.getEndDate());
        }

        wrapper.orderByAsc(Category::getSortOrder);

        // 执行查询
        List<Category> categories = categoryMapper.selectList(wrapper);

        List<CategoryVO> vos = categories.stream()
                .map(category -> {
                    CategoryVO vo = new CategoryVO();
                    BeanUtils.copyProperties(category, vo);
                    return vo;
                }).collect(Collectors.toList());
        return vos;

    }
}




